<?php
/*
*
* @author rana
*
*/

// Strip out javascript, styles etc..
$GLOBALS['link_regexp'] = array ("/javascript\s*:/si","/^#/si","/return\s+/si");              

class LinkChecker {

	var $scheme;  //- e.g. http 

	var $host; 

	var $port; 

	var $user; 

	var $pass; 

	var $path; 

	var $query; //- after the question mark ? 

	var $fragment; //- after the hashmark # 


	function LinkChecker($url) {
		$info = parse_url($url);
		$this->scheme = @$info['scheme']; 
		$this->host = @$info['host'];
		$this->port = @$info['port'];
		$this->user = @$info['user'];
		$this->pass = @$info['pass'];
		$this->path = @$info['path'];
		$this->query = @$info['query'];
		$this->fragment = @$info['fragment'];
		//echo $this->scheme."/".$this->host."/".$this->port;
	}

	function get_links($link_data, $temp_dup=array()) {
		global $BASE_URL, $MY_LINKS;

		foreach ($link_data as $link) {

			$attribs = $link['attributes'];
			$href = trim($attribs['href']);

			if (empty($href) ) continue;
			if (in_array($href, $MY_LINKS)) continue;

			if ($this->is_well_link($href)) {

				$full_href = $this->get_url_with_base($BASE_URL, $href);

				$MY_LINKS[] = $full_href;
			}

		}

		return $MY_LINKS;
	}

	//
	function is_well_link($link) {

		foreach ($GLOBALS['link_regexp'] as $regex) {
			$found = preg_match($regex, $link);
			if ($found > 0)
				return false;
		}
		$is_inboud_link = $this->check_inbound($link);

		return $is_inboud_link;
	}
	
	//
	function check_inbound($link) {
		$flag = false;

		$info = parse_url($link);
		
		if ( empty($info['scheme']) || $info['scheme'] == 'http' || $info['scheme'] == 'https' )
			$flag = true;
		
		if ( $flag && ( empty($info['host']) || $info['host'] == $this->host) )
			$flag = true;
		else
			$flag = false;

		if ( empty($info['scheme']) && empty($info['host']) && empty($info['path']) && !empty($info['fragment']) )
			$flag = false;
		
		//if calling a javascript function
		if ( empty($info['scheme']) && empty($info['host']) && !empty($info['path']) ) {
			if (preg_match("/[a-zA-Z_]\s*\(.*\)/i",$info['path']) )
				$flag = false;
		}
		return $flag;
	}

	function get_base() {
		$site = '';

		if (empty($this->scheme)){
			$this->scheme = 'http';
		}
		$site = $this->scheme.'://';

		if (empty($this->host)){
			$paths = explode('/', $this->path);
			foreach($paths as $path) {
				$path = trim($path);
				if (!empty($path)) {
					$site .= $path;
					break;
				}
			}
		} else {
			$site .= $this->host;
	    }

		if (!empty($this->port) ){
			$site .= ':'.$this->port;
		}
		return $site;
	}

	function get_full_url($url) {
		$str1 = explode("://",$url);
		$site = '';

		if ( count($str1) == 2  ) {

			if ($str1[0] == 'http' || $str1[0] == 'https') {
				$scheme = $str1[0];
			}
			else {
				$scheme = 'http';
			}
			$host = $str1[1];
		} else {

			$scheme = 'http';
			$host = $str1[0];
		}
		
		$paths = explode('/', $host);
		foreach($paths as $path) {

			$path = trim($path);

			if (!empty($path)) {
				$site .= $path.'/';
			}
		}

		if (substr($site,-1) == '/' )
				$site = substr($site,0,-1);
		return $scheme . '://' . $site;
	}

	function get_url_with_base($base, $url) {
		$site = '';
		$info = parse_url($url);
		$binfo = parse_url($base);

		$site = $binfo['scheme'] .'://'.$binfo['host'];

		if (!empty($binfo['port']) ){
			$site .= ':'.$binfo['port'];
		}

		if (!empty($info['path']) ){
			if (substr($info['path'],0,1) != '/' )
				$site .= '/';
			$site .= $info['path'];
		}
		if (!empty($info['query']) ){
			$site .= '?'.$info['query'];
		}
		if (!empty($info['fragment']) ){
			$site .= '#'.$info['fragment'];
		}
		return $site;
	}

	//
	function remove_base($page) {
		//$page = str_replace($GLOBALS['URL_INFO']['BASE'],"",$page);
		$page = trim($page);
		$info = parse_url($page);
		$site = '/';

		if (!empty($info['path']) && $info['path'] != '/' ){
			$site .= '/' . $info['path'];
		}
		if (!empty($info['query']) ){
			$site .= '?'.$info['query'];
		}
		if (!empty($info['fragment']) ){
			$site .= '#'.$info['fragment'];
		}
		$start = substr($site,0,1);
		while ($start == '/'){
			$site = substr($site,1);
			$start = substr($site,0,1);
		}
		return $site;
	}
}
?>